package com.kelvin.oocl.testTransaction.dao.impl;

import com.kelvin.oocl.testTransaction.tool.DBUtils;
import com.kelvin.oocl.testTransaction.dao.EmployeeDao;
import com.kelvin.oocl.testTransaction.exception.DBOperationException;
import com.kelvin.oocl.testTransaction.model.Employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class EmployeeDaoImpl implements EmployeeDao{
    @Override
    public int save(Employee emp) {
        Connection connection = null;
        PreparedStatement ps = null;
        connection = DBUtils.getConnection();
        String sql = "insert into emp(id,name,addr) values(?,?,?)";
        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1, emp.getId());
            ps.setString(2, emp.getName());
            ps.setString(3, emp.getAddr());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new DBOperationException("EmployeeDao operation failed.");
        }
    }
}
